' ------------------------------------------------'
' CreateFolder.vbs
' Create new folder and copy files
' Author TinhTD@ POSCOICT-VietNam
' Version 1.0 - Mar,25th,2014
' ------------------------------------------------'
Option Explicit
Dim objFSO, objFolder, objShell, strThumuc,strCurDate, strDay,strfoMonth,strfoDay,strRDate,i
Dim strCopyFrom,strfMonth,strfDay,strMainFolder,f,strDelFolder,dStatus,fName,strFDate,strFODate,strOday

strCurDate = Now()
strfMonth = Right(String(2, "0") & Month(strCurDate), 2)
strfDay = Right(String(2, "0") & Day(strCurDate), 2)
strDay = Year(strCurDate) & strfMonth & strfDay
strMainFolder="C:\SPP_BACKUP"
strThumuc = "C:\SPP_BACKUP\BACKUP_" & strDay
strDelFolder="BACKUP_" & strDay
strCopyFrom = "D:\SSVINA_SPP\Code"

'Get old date
Function GetOldDate(strVar)

	strFODate = DateAdd("m",-strVar,strCurDate)
	strfoMonth = Right(String(2, "0") & Month(strFODate), 2)
	strOday = Year(strFODate) & strfoMonth

	GetOldDate = strOday
End Function
'End get old date

'Delete old data
	For Each f In CreateObject("Scripting.FileSystemObject").GetFolder(strMainFolder).SubFolders
		fName = Mid(f.Name,8,6) 
			For i = 6 To 0 Step -1	
				strRDate = GetOldDate(i)
				If fName = strRDate Then
					f.Delete
					
				End If
			Next
	
	Next
	WScript.Echo "All backup version 6 month ago has been deleted"
'End delete
'Create new folder
	' Create the File System Object
	Set objFSO = CreateObject("Scripting.FileSystemObject")

	'Checking filderexist
	If objFSO.FolderExists(strThumuc) Then
	   Set objFolder = objFSO.GetFolder(strThumuc)
	   WScript.Echo strThumuc & " already created "
	   objFSO.CopyFolder strCopyFrom, strThumuc
	Else
		'Create new folder
		Set objFolder = objFSO.CreateFolder(strThumuc)
		WScript.Echo "Just created " & strThumuc
		'Copy folder from to
		objFSO.CopyFolder strCopyFrom, strThumuc
	End If
'End create 

'Error
If err.number = vbEmpty then
   'Set objShell = CreateObject("WScript.Shell")
   'objShell.run ("Explorer" &" " & strThumuc & "\" )
Else WScript.echo "VBScript Error: " & err.number
End If

WScript.Quit